//index.js
//获取应用实例
const {
  weixin
} = require('../../utils/value.js');
const app = require('../../utils/ajax.js');
const config = require('../../utils/config.js');

Page({
  data: {
    imgUrls: ['../image/u17.png', '../image/u17.png', '../image/u17.png'],
    serviceList: [{
        name: '住家陪护',
        img: '../image/u1.png'
      },
      {
        name: '康复训练',
        img: '../image/u2.png'
      },
      {
        name: '旅游',
        img: '../image/u3.png'
      },
      {
        name: '美食',
        img: '../image/u4.png'
      },
      {
        name: '商场',
        img: '../image/u6.png'
      },
      {
        name: '娱乐',
        img: '../image/u9.png'
      }
    ],
    hospitalInfo: {},
    advertImg: '../image/u24.png'
  },

  onLoad: function(options) {
    //this.handleGetOpenId();
    this.handleGetLocation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const hospitalInfo = app.getData('hospitalInfo');
    console.log(JSON.parse(hospitalInfo));
    this.setData({
      hospitalInfo: JSON.parse(hospitalInfo)
    });
  },

  onPullDownRefresh: function() {
    //解决下拉不回弹
    wx.stopPullDownRefresh();
  },

  //转发
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮 
      console.log(res.target)
    }
    return {
      title: '医院后勤管理',
      path: '/pages/index/index',
      success: function(res) {
        // 转发成功
        console.log("成功")
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },

  /**
   * 跳转到选择医院页面
   */
  handleRedirectToLocation() {
    wx.navigateTo({
      url: '../location/location',
    })
  },

  /**
   * 跳转到结算页
   */
  handleRedirectToSettleAccount: function(e) {
    const type1 = e.currentTarget.dataset.type;
    if (type1 !== '3') {
      wx.showToast({
        title: '该功能尚未开放',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (type1 === '3') {
      const url = config.ServerUrl + weixin.SELECT_BED;
      console.log(url);
      app.doGet(url, {
        weixin_id: app.getData('open_id')
      }, function(res) {
        console.log(res);
        if (res.state === 1) {
          wx.navigateTo({
            url: '../order/settleAccount',
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          })
        }
      })
    }
  },

  /**
   * 获取当前经纬度
   */
  handleGetLocation: function(openid) {
    const _this = this;
    wx.showLoading({
      title: '定位中...',
    })
    wx.login({
      success: function(res) {
        if (res.code) {
          wx.getLocation({
            success: function(res1) {
              _this.handleGetAddress(res1.latitude, res1.longitude, res.code);
            },
            fail: function() {
              wx.showToast({
                title: '定位失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function(err) {
        wx.showToast({
          title: '登录失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 根据经纬度获取具体地址
   */
  handleGetAddress: function(latitude, longitude, code) {
    const param = {
      resultlng: 113.1729,
      result1lat: 23.02756,
      code: code
    }
    const that = this;
    app.doPost(weixin.GPS, param, function(res) {
      if (res.state === 1) {
        const {
          auth,
          advertise_image,
          detailaddress,
          hospital_name,
          image,
          open_id
        } = res.data;
        const imgUrls = [];
        for (let i = 0; i < image.length; i++) {
          imgUrls[i] = config.imageServerUrl + image[i];
        }
        const hospitalInfo = {
          hospital_name,
          hospital_id: '',
          detailaddress,
          advertise_image: config.imageServerUrl + advertise_image,
          hospital_picture: imgUrls
        }
        console.log(hospitalInfo);
        that.setData({hospitalInfo});
        app.setData('hospitalInfo', hospitalInfo);
        app.setData('open_id', open_id);
        app.setData('token', auth);
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
        //const open_id=res.data.open_id;
        //if(open_id){
        app.setData('open_id','');
        //}
        const hospitalInfo={
          hospital_name: '定位失败',
          hospital_id: '',
          detailaddress: '',
          advertise_image: '../image/u24.png',
          hospital_picture: ['../image/u17.png', '../image/u17.png', '../image/u17.png']
        }
        that.setData({hospitalInfo});
        console.log(hospitalInfo);
        app.setData('hospitalInfo', hospitalInfo);
        setTimeout(function() {
          wx.navigateTo({
            url: '../location/location',
          })
        }, 500)
      }
    })
  },

  /**
   * 跳转到搜索页面
   */
  handleRedirectToSearch: function() {
    wx.showToast({
      title: '该功能尚未开放',
      icon: 'none',
      duration: 1000
    })
  },

  /**
   * 跳转到推荐服务页
   */
  handleRecirectToService: function() {
    wx.showToast({
      title: '该功能尚未开放',
      icon: 'none',
      duration: 1000
    })
  }
})